<template>
  <div class="login-page">
    <van-nav-bar
      left-text="返回"
      right-text="更多"
      left-arrow
      @click-left="onClickLeft"
      @click-right="onClickRight"
    />
    <div class="body">
      <div class="head-image">
        <img src="../../static/images/defaultHeadImage.jpg"/>
      </div>
      <div class="form">
        <Input class="item" prefix="md-person" placeholder="用户名/手机号/邮箱" v-model="user.uniqueString"/>
        <Input class="item" prefix="md-lock" placeholder="请输入密码" type="password" v-model="user.password"
               @keyup.enter.native="login"/>
        <Button class="item" type="primary" @click="login">登陆</Button>
      </div>
    </div>
  </div>
</template>
<script>

  export default {
    data() {
      return {
        isLoad: false,
        user: {
          uniqueString: "ychen", /** 唯一属性*/
          password: "ychen"
        }
      };
    },
    methods: {
      onClickLeft() {
        this.$router.back(-1)
      },
      onClickRight() {
      },
      login: function () {
        let that = this
        let url = 'user-api/user/login'

        const loading = this.$loading({
          lock: true,
          text: '正在登陆',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });

        this.$http({
          url: this.$http.adornUrl(url),
          method: 'post',
          data: that.user
        }).then((result) => {
          loading.close();
          console.log(result)
          if (result && result.code === 0) {
            that.$store.state.user.userInfo = result.userInfo
            that.$cookie.set('token', result.token)
            if (this.$route.query.goindex === 'true') {
              this.$router.push('/')
            } else {
              this.$router.back(-1)
            }
          } else {
            this.$message.error(result.msg);
          }
        })
      }
    }
  }
</script>

<style lang="less" scoped>
  @import "../assets/less/index";
</style>
